package com.corpgovernment.supplier.service;

import com.corpgovernment.api.supplier.soa.request.QueryEnableCorpPayTypeRequest;
import com.corpgovernment.api.supplier.soa.response.MbSupplierCompanyVoResponse;
import com.corpgovernment.api.supplier.vo.MbSupplierCompanyVo;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.common.common.CorpBusinessException;
import com.corpgovernment.common.enums.LogLevelEnum;
import com.corpgovernment.common.logging.LogUtil;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.corpgovernment.organization.constant.OrganizationResponseCodeEnum;
import com.corpgovernment.organization.entity.db.MbOrgInfo;
import com.corpgovernment.organization.service.IOrganizationInfoService;
import com.corpgovernment.supplier.entity.db.SupplierCompany;
import com.corpgovernment.supplier.enums.PubOwnOptionEnum;
import com.corpgovernment.supplier.mapper.SupplierCompanyMapper;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @auth:yaqiangzhang
 * @date:2019/7/29 17:15
 */
@Service
@Slf4j
public class MbSupplierCompanyService {

	@Autowired
	private SupplierCompanyMapper supplierCompanyMapper;
	@Autowired
	private IOrganizationInfoService organizationInfoService;

    /**
     * 获取是否有提供服务的因公因私供应商
     */
    public JSONResult<List<String>> queryEnableCorpPayType(QueryEnableCorpPayTypeRequest request) {
        Objects.requireNonNull(request.getCorpID());
        Objects.requireNonNull(request.getProductType());
        Set<String> corpPayTypes = new HashSet<>();
        Example e = new Example(SupplierCompany.class);
        e.createCriteria().andEqualTo("productType", request.getProductType())
                .andEqualTo("isDeleted", false)
                .andEqualTo("companyCode", request.getCorpID());
        for (SupplierCompany supplierCompany : supplierCompanyMapper.selectByExample(e)) {
            if (supplierCompany.getBusPriType() == PubOwnOptionEnum.PUB_OR_OWN.getValue()) {
                corpPayTypes.add("PUB");
                corpPayTypes.add("OWN");
            } else if (supplierCompany.getBusPriType() == PubOwnOptionEnum.PUB.getValue()) {
                corpPayTypes.add("PUB");
            } else if (supplierCompany.getBusPriType() == PubOwnOptionEnum.OWN.getValue()) {
                corpPayTypes.add("OWN");
            }
        }
        return JSONResult.success(Lists.newArrayList(corpPayTypes));
    }

    public String queryRelatedUsefulOrg(String supplierCode) {
	    if (StringUtils.isBlank(supplierCode)) {
		    throw new CorpBusinessException(OrganizationResponseCodeEnum.SUPPLIER_CODE_NULL);
	    }
        Example e = new Example(SupplierCompany.class);
        e.createCriteria()
                .andEqualTo("supplierCode", supplierCode)
                .andEqualTo("isDeleted", false);
        List<SupplierCompany> c = supplierCompanyMapper.selectByExample(e);
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(c)) {
			MbOrgInfo mbOrgInfo = organizationInfoService.findByOrgId(c.get(0).getCompanyCode());
			if (mbOrgInfo == null) {
				log.error("查询组织架构失败");
                throw new CorpBusinessException(OrganizationResponseCodeEnum.GET_ORG_INFO_NULL);
			}
			return mbOrgInfo.getName();
		}
        return null;
    }

    /**
     * 获取供应商信息
     *
     * @param request
     * @return
     */
    public JSONResult<MbSupplierCompanyVoResponse> getMbSupplierCompany(MbSupplierCompanyVo request) {
        LogUtil.logging(LogLevelEnum.Info, MbSupplierCompanyService.class, "入参", "参数如下", new HashMap<String, String>() {{
            put("request", JsonUtils.toJsonString(request));
        }});
        SupplierCompany supplierCompany = new SupplierCompany();
        BeanUtils.copyProperties(request, supplierCompany);
        supplierCompany.setIsDeleted(Boolean.FALSE);
        List<SupplierCompany> supplierCompanies = supplierCompanyMapper.select(supplierCompany);
        if (CollectionUtils.isEmpty(supplierCompanies)){
            return null;
        }
        List<MbSupplierCompanyVo> companyVos = new ArrayList<>();
        supplierCompanies.forEach(item->{
            MbSupplierCompanyVo companyVo = new MbSupplierCompanyVo();
            BeanUtils.copyProperties(item, companyVo);
            companyVos.add(companyVo);
        });
        return new JSONResult<>(new MbSupplierCompanyVoResponse(companyVos));
    }

}
